/*
 * Created on May 8, 2005
 *
 */
package org.mushroomdb.catalog.impl;

import java.util.Iterator;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.Register;
import org.mushroomdb.catalog.WriteBackRegister;
import org.mushroomdb.filemanagement.Page;
import org.mushroomdb.util.MultipleIterator;


/**
 * @author Matias
 *
 */
public class MultipleRegister implements WriteBackRegister {
	private Register leftRegister;
	private Register rightRegister;

	/**
	 * Construye un registro a partir de otros dos.
	 */
	public MultipleRegister(Register leftRegister, Register rightRegister) {
		super();
		this.leftRegister = leftRegister;
		this.rightRegister = rightRegister;
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumns()
	 */
	public Iterator getColumns() {
		return new MultipleIterator(
				this.leftRegister.getColumns(),
				this.rightRegister.getColumns());
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnIndex(java.lang.String)
	 */
	public int getColumnIndex(Column column) {
		int leftColumIndex = this.leftRegister.getColumnIndex(column);
		if (leftColumIndex > 0) {
			return leftColumIndex;
		} else {
			int rightIndex = this.rightRegister.getColumnIndex(column);
			if (rightIndex > 0)
				return rightIndex + this.leftRegister.getColumnCount();
			else
				return -1;
		}
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getValue(int)
	 */
	public Object getValue(int columnIndex) {
		if (columnIndex <= this.leftRegister.getColumnCount()) {
			return this.leftRegister.getValue(columnIndex);
		} else {
			return this.rightRegister.getValue(columnIndex - this.leftRegister.getColumnCount());
		}
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnCount()
	 */
	public int getColumnCount() {
		return this.leftRegister.getColumnCount() + this.rightRegister.getColumnCount();
	}

	/**
	 * @see org.mushroomdb.catalog.Register#setValue(int, java.lang.Object)
	 */
	public void setValue(int columnIndex, Object value) {
		if (columnIndex <= this.leftRegister.getColumnCount()) {
			this.leftRegister.setValue(columnIndex,value);
		} else {
			this.rightRegister.setValue(columnIndex - this.leftRegister.getColumnCount(),value);
		}
	}

	/**
	 * toString
	 */
	public String toString() {
		int columnCount = this.getColumnCount();
		String rv = ""; //$NON-NLS-1$
		
		for (int i = 1; i <= columnCount; i++) {
			if (rv.length() > 0) {
				rv = rv + ',';
			}
			
			Object object = this.getValue(i).toString();
			if (object instanceof String) {
				rv = rv + '\'' + object.toString() + '\'';
			} else {
				rv = rv + object.toString();
			}
		}
		
		return '(' + rv + ')';
	}

	/**
	 * @see org.mushroomdb.catalog.Register#remove()
	 */
	public void remove() {
		throw new UnsupportedOperationException("This register cannot be removed."); //$NON-NLS-1$
	}

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#setPage(org.mushroomdb.filemanagement.Page)
     */
    public void setPage(Page page) {
        // TODO Auto-generated method stub
        
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#setOffset(int)
     */
    public void setOffset(int offset) {
        // TODO Auto-generated method stub
        
    }

    //TODO: MERGE VER ESTO
    public int getOffset() {
        // TODO Auto-generated method stub
        return 0;
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#getPage()
     */
    public Page getPage() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.WriteBackRegister#isInTrx()
     */
    public boolean isInTrx() {
        // TODO Auto-generated method stub
        return false;
    }

  
}
